import { login, logout, getInfo } from '@/api/user'
import {getAuthorInfo} from "@/api/author";
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
import store from '@/store'


const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: '',
    addRoutes:[]
  }
}

/**
 * 后台查询的菜单数据拼装成路由格式的数据
 * @param routes
 */
 function generaMenu(routes, data) {
  
  data.forEach(item => {
     
    // alert(JSON.stringify(item))
    const menu = {
      path: item.routerPath ,
      component: item.children.length > 0 ? Layout : (resolve) => require([`@/views${item.routerPath}/index`],resolve),
      // hidden: true,
      children: [],
      name: item.authName,
      meta: { title: item.authName, id: item.menuId, roles: ['admin'],icon: item.logo}
    }
    if (item.children) {
      generaMenu(menu.children, item.children)
    }
    routes.push(menu)

  })
    return routes
}
 

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes
  },
  SET_AUTHOR: (state, author) => {
    state.author_Info = author
  }

}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password }).then(response => {
        const  data  = response
        console.log(data)
        commit('SET_TOKEN', data.headers.authorization)
        setToken( data.headers.authorization)
        // this.$router.options.routes = store.getters.addRoutes
        
        resolve()
             
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
    
      getInfo(state.token).then(response => {
        const { data } = response.data

        if (!data) {
          return reject('Verification failed, please Login again.')
        }
        console.log(data)
        const { username, avatar } = data
        
        commit('SET_NAME', username)
        commit('SET_AVATAR', avatar)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      removeToken()
      commit('RESET_STATE')
      logout().then(() => {
        // must remove  token  first
        // resetRouter()  //重置路由
      
        // console.log(store)
       
        resolve()
     
      }).catch(error => {
        reject(error)
      })
    })
  },

  //获取开发人员信息
  getDevelopers({ commit, state }) {
    return new Promise((resolve, reject) => {
      getAuthorInfo().then((res) => {
       const data = res.data.data
        commit('SET_AUTHOR', data)
        resolve(data)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

